﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fis.Win.Utils;

namespace Fis.Win.Forms
{
    public partial class AidsEditForm : EditForm
    {
        Dictionary<string, DevExpress.XtraEditors.TextEdit> registry
            = new Dictionary<string, DevExpress.XtraEditors.TextEdit>();

        public AidsEditForm()
        {
            InitializeComponent();

            registry["FCODE"] = FCODE;
            registry["FNAME"] = FNAME;
            registry["FGROUP"] = FGROUP;
            registry["FDEFAULT"] = FDEFAULT;
        }

        public override void InitializeBinding()
        {
            var d = ViewModel["viewData"] as DataTable;
            ViewModel["Action"] = ViewState.ToString();

            UIUtils.ClearBindings(layoutControl1);
            foreach (var i in registry.AsEnumerable())
            {
                i.Value.DataBindings.Add("EditValue", d, i.Key);
                i.Value.EnterMoveNextControl = true;
            }

            d.Rows[0]["FGROUP"] = ViewModel["FGROUP"];
            UIUtils.LookUpBinding(FDEFAULT, CacheContext.YesNoCache(), "display", "value");

            if (ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                d.Rows[0]["FDEFAULT"] = 0;
            }

            FCODE.Properties.ReadOnly = (ViewState != Agile.Engine.View.ViewState.AddNew);
            FCODE.Focus();
        }

        public override void InitializeViewUI()
        {
            var d = UIUtils.ViewConfig.Tables["aidscolumn"];
            var g = ViewModel["FGROUP"];

            foreach (var i in d.Select(string.Format("group='{0}'", g)))
            {
                CreateControl(i);
            }

            emptySpaceItem1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }

        protected override void InitializeAfterSave()
        {

            System.Collections.Hashtable m = new System.Collections.Hashtable();
            m["FGROUP"] = ViewModel["FGROUP"];

            ViewModel = ViewProvider.InitializeViewModel(m);
            InitializeBinding();

            //foreach (var i in registry.AsEnumerable())
            //{
            //    i.Value.EditValue = string.Empty;
            //}

            //FCODE.Focus();
        }

        protected override bool SaveProcess()
        {
            ///失败
            if (!ViewProvider.ExecuteUpdate(ViewModel))
            {
                return false;
            }

            Message = "保存成功";
            return true;
        }


        private void CreateControl(DataRow row)
        {
            var c = row["caption"].ToString();
            var f = row["field"].ToString();
            var t = row["type"].ToString();
            var i = int.Parse(row["index"].ToString());

            DevExpress.XtraEditors.TextEdit ctl = null;
            switch (t)
            {
                case "string":
                    ctl = new DevExpress.XtraEditors.TextEdit();
                    break;
                case "number":
                    ctl = new DevExpress.XtraEditors.SpinEdit();
                    break;
                case "date":
                    ctl = new DevExpress.XtraEditors.DateEdit();
                    break;
            }

            var item = new DevExpress.XtraLayout.LayoutControlItem(layoutControl1, ctl);
            layoutControlGroup1.AddItem(item);

            item.Location = new System.Drawing.Point(0, i * 24);
            item.Name = f;
            item.Size = new System.Drawing.Size(294, i);
            item.Text = c;
            item.TextSize = new System.Drawing.Size(i, 13);
            registry[f] = ctl;
        }
    }
}
